package com.sgb.member.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.member.R;
import com.sgb.member.databinding.ItemOtherEnterpriseServiceBinding;
import com.sgb.member.entity.MemberServiceListBean;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

public class MemberServiceListAdapter extends BaseRecycleAdapter<MemberServiceListBean> {
    RequestOptions options;
    RequestOptions teamOptions;
    boolean flag = false; //区分首页及企业服务是否显示关注按钮 false 不显示

    public MemberServiceListAdapter(Context context, List<MemberServiceListBean> datas) {
        super(context, R.layout.item_other_enterprise_service, datas);
        options = new RequestOptions().placeholder(R.drawable.icon_default_shop)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_default_shop) //url为空的时候,显示的图片
                .error(R.drawable.icon_default_shop);//图片加载失败后，显示的图片
        teamOptions = new RequestOptions().placeholder(R.drawable.icon_wulogo_team_heand)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_wulogo_team_heand) //url为空的时候,显示的图片
                .error(R.drawable.icon_wulogo_team_heand);//图片加载失败后，显示的图片
    }

    @Override
    public void convert(ItemViewHolder holder, MemberServiceListBean serviceListEntiy) {
        ItemOtherEnterpriseServiceBinding binding = (ItemOtherEnterpriseServiceBinding) holder.getmBinding();
        binding.setModel(serviceListEntiy);
       /* if (flag) {
            binding.tvFollow.setVisibility(View.VISIBLE);
        } else {
            binding.tvFollow.setVisibility(View.GONE);
        }
        setFollwType(binding.tvFollow, serviceListEntiy.isAttention());*/

        if (!TextUtils.isEmpty(serviceListEntiy.getAuthTime())) {
            try {
                long i = Long.parseLong(serviceListEntiy.getAuthTime()) / (24 * 60 * 60 * 1000);
                if (i < 365) {
                    binding.tvTypeBj.setText(String.format("入驻%s天", i));
                } else {
                    binding.tvTypeBj.setText("入驻" + (i / 365) + "年");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        binding.tvFollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClick != null) {
                    onItemClick.onItemClick(serviceListEntiy, -1);
                }
            }
        });
        try {
            String service = "/";
            if (!TextUtils.isEmpty(serviceListEntiy.getServiceScope()) && serviceListEntiy.getServiceScope().indexOf("[") > -1) {

                com.alibaba.fastjson.JSONArray jsonArray = com.alibaba.fastjson.JSONArray.parseArray(serviceListEntiy.getServiceScope());
                if (jsonArray.size() > 0) {
                    service = "";
                }
                for (int i = 0; i < jsonArray.size(); i++) {
                    com.alibaba.fastjson.JSONObject jsonObject = jsonArray.getJSONObject(i);
                    if (jsonObject.get("name") != null) {
                        service = service + jsonObject.getString("name") + "、";
                    }
                }
                if (!TextUtils.isEmpty(service) && service.indexOf("、") > -1) {
                    service = service.substring(0, service.length() - 1);
                }

            }

            binding.tvServiceFw.setText(service);

        } catch (Exception e) {
            e.printStackTrace();
        }
        Glide.with(context).load(serviceListEntiy.getImg()).apply(options).into(binding.ivPublishType);
    }


    private void setFollwType(TextView textView, boolean flag) {
        textView.setText(flag ? "已关注" : "关注");
        textView.setBackgroundResource(flag ? R.drawable.member_shape_rectangle_gray_5 : R.drawable.member_shape_rectangle_red_5);
        textView.setTextColor(flag ? Color.parseColor("#969696") : Color.parseColor("#EF4033"));
        textView.setTextColor(flag ? Color.parseColor("#969696") : Color.parseColor("#ef4033"));


    }
}
